package com.sec.etech.ism.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.*;
import com.sec.etech.sys.util.PropertyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openbpm.base.api.exception.BusinessException;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class IsmOmpProcessServiceImpl implements IsmMethodService {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmTaskService ismTaskService;
    @Resource
    IsmServiceDefService ismServiceDefService;
    @Resource
    IsmTaskLogService ismTaskLogService;
    @Resource
    MaterialNoLogService materialNoLogService;
    @Resource
    IsmCommonService ismCommonService;


    public String postEtechData(String jsonString){
        if (PropertyUtil.getPropertyService().getByAlias("systemEnvironmentVariables").equals("dev")) {
            HttpResponse response = HttpUtil.createPost("http://10.9.8.162:6111/api/manufactureOrderIssue/startBpmFromOmp")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("Accept", "application/json, text/plain, */*")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .body(jsonString)
                    .execute();
            JSONObject resp = JSONObject.parseObject(response.body());
            return resp.getString("data");
        }
        if (PropertyUtil.getPropertyService().getByAlias("systemEnvironmentVariables").equals("prod")) {
            HttpResponse response = HttpUtil.createPost("http://etech.stcetech.ad01.sec.com/api/manufactureOrderIssue/startBpmFromOmp")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("Accept", "application/json, text/plain, */*")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .body(jsonString)
                    .execute();
            JSONObject resp = JSONObject.parseObject(response.body());
            return resp.getString("data");
        }
        return "未知环境！";
    }

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {

        JSONObject params = businessParams.getJSONObject("input_params");
        String project = params.getString("project");

        IsmServiceDef serviceDef = ismServiceDefService.getByCode(serviceCode);
        IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);


        if (StringUtil.isEmpty(project)){
            throw new BusinessException("缺少必要参数project,请填写以太客编号PROJXXXXX!");
        }
        if (!project.startsWith("PROJ")){
            throw new BusinessException("请填写以太客编号PROJXXXXX!");
        }


        JSONArray deliveryTime = params.getJSONArray("deliveryTime");
        if (deliveryTime == null || deliveryTime.size() == 0){
            throw new BusinessException("缺少必要参数deliveryTime");
        }

        for (int i = 0; i < deliveryTime.size();i++){
            JSONObject de = deliveryTime.getJSONObject(i);
            if (de.getString("date") == null){
                throw new BusinessException("缺少必要参数deliveryTime -> date");
            }
        }

        JSONArray techFileList = params.getJSONArray("techFileList");
        if (techFileList == null || techFileList.size() == 0){
            throw new BusinessException("缺少必要参数techFileList");
        }




        for (int i = 0; i < techFileList.size();i++){
            JSONObject tech = techFileList.getJSONObject(i);
            if (StringUtils.isBlank(tech.getString("url"))){
                throw new BusinessException("缺少必要参数techFileList -> url");
            }
            if (StringUtils.isBlank(tech.getString("name"))){
                throw new BusinessException("缺少必要参数techFileList -> name");
            }
        }


        JSONArray contractFileList = params.getJSONArray("contractFileList");
        if (contractFileList == null || contractFileList.size() == 0){

        }else{
            for (int i = 0; i < contractFileList.size();i++){
                JSONObject tech = contractFileList.getJSONObject(i);
                if (StringUtils.isBlank(tech.getString("url"))){
                    throw new BusinessException("缺少必要参数contractFileList -> url");
                }
                if (StringUtils.isBlank(tech.getString("name"))){
                    throw new BusinessException("缺少必要参数contractFileList -> name");
                }
            }
        }

        JSONObject result = new JSONObject();

        String res = postEtechData(JSONObject.toJSONString(params));
        if ("success".equals(res)){
            result.put("data", "调用成功");
            result.put("flag", "success");
        }else {
            result.put("data", res);
            result.put("flag", "fail");
        }

        JSONObject logObj = new JSONObject();
        logObj.put("service_code", serviceDef.getCode());
        logObj.put("service_name", serviceDef.getName());
        logObj.put("method_name", "ompProcess");
        logObj.put("input_json", businessParams);
        logObj.put("output_json", result);
        ismTaskLogService.saveLog(logObj);


        if ("fail".equals(result.getString("flag"))){
            throw new BusinessException(result.getString("data"));
        }


        return result;
    }

    @Override
    public String methodName() {
        return "ompProcess";
    }
}
